package com.zhaojp.demo.controller;

import com.alibaba.fastjson.JSON;
import com.zhaojp.demo.config.Constants;
import com.zhaojp.demo.domain.User;
import com.zhaojp.demo.helper.RedisService;
import com.zhaojp.demo.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RestController
@Controller
public class IndexController {

    private static Logger LOGGGER = LoggerFactory.getLogger(IndexController.class);

    @Resource
    private Constants constants;

    @Resource
    private UserService userService;

    @Resource
    private RedisService redisService;

    @RequestMapping(value = "/helloworld", method = RequestMethod.GET)
    @ResponseBody
    public Map helloworld() {
        Map rs = new HashMap();
        rs.put("1", constants.getUserName());
        rs.put("2", 2);
//        BeanUtils.populate(rs,new Object);
        return rs;
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, HttpRequest request) {
        model.addAttribute("msg", "Hello Freemarker");
        return "index/index";
    }

    @RequestMapping(value = "/register", method = RequestMethod.PUT)
    @ResponseBody
    public User register() {
        User user = new User();
        String rs = redisService.getStr("userId");
        if(StringUtils.isBlank(rs)){
            rs = "1";
            redisService.setStr("userId",rs);
        }
        user.setId(Integer.parseInt(rs)+1);
        redisService.setStr("userId",user.getId()+"");
        user.setId(Integer.parseInt(rs)+1);//(int)Math.floor(Math.random()*1000)
        user.setName("张飞");
        user.setBirthday(new Date());
        //fastjson检测
        String  js = JSON.toJSONString(user);
        User user1 = JSON.parseObject(js, User.class);
        //发布事件到servlet
        userService.register(user);
        LOGGGER.info("pub event success!");
        return user1;
    }

    @ApiOperation(value = "分页获取用户数据", notes = "根据不同page和size获取数据", httpMethod = "GET",response = User.class, responseContainer = "List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "p", value = "页码从1开始", required = true, dataType = "Integer", paramType="query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", required = true, dataType = "Integer", paramType="query")
    })
    @RequestMapping(value = "/service", method = RequestMethod.GET)
    @ResponseBody
    public List<User> service(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                              @RequestParam(value = "size", required = false, defaultValue = "2") int size) {
        LOGGGER.info("Begin service...");


        return userService.getUserInfo(p, size);
    }

}
